import random
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.db import connection
import requests
import json
import time
import os
from django.http import HttpResponseRedirect
from django.urls import reverse
from yxb import common_tools
 
 

def index111111111122211111(request):
    file_id = request.GET.get('file_id', None)
    print(file_id)
    id_listtt = get_now_pre_next_id(file_id)
    now_item_file_id = id_listtt[0]
    pre_item_file_id = id_listtt[1]
    next_item_file_id = id_listtt[2]
    alll_contentt = get_file_content(now_item_file_id)
    return_data = {
        'title':' 大文本排版换行测试 ( and SQL 测试) : ',
        'file_id': file_id ,
        "aaa" : json.dumps(aaa),
        "all_content" : json.dumps(alll_contentt),
        "pre_item_file_id" :  pre_item_file_id,
        "next_item_file_id" : next_item_file_id ,
        "get_file_level_400_500" : get_file_level_400_500(),
    }
    return render(request, 'edit/index.html', context=return_data)



@csrf_exempt
def delete_ajax(request):
    if request.method == 'POST':
        post_data = json.loads(request.body.decode('utf-8'))
        file_id = post_data.get('file_id', '')
        print(file_id)
        current_path = os.path.abspath(__file__)
        parent_folder = os.path.dirname(os.path.dirname(current_path))
        file_full_path = parent_folder + "\TXT\\" + file_id
        print(file_full_path)
        os.remove(file_full_path)
        return HttpResponse("success", content_type="application/json")
    else:
        return HttpResponse("false", content_type="application/json")






# @csrf_exempt
# def ajax_new(request):
#     if request.method == 'POST':
#         post_data = json.loads(request.body.decode('utf-8'))
#         string_data = post_data.get('string_data', '')
#         select_value = post_data.get('select_value', '')
#         print(string_data)
#         print(select_value)
#         current_path = os.path.abspath(__file__)
#         parent_folder = os.path.dirname(os.path.dirname(current_path))
#         print("父文件夹:  ")
#         print(parent_folder + "\TXT")
#         time_stamp = str(round(time.time() * 1000))   # 1700477634782 时间戳
#         file_full_path = parent_folder + "\TXT" + "\\" + str(time_stamp) + ".txt"
#         print(file_full_path)
#         common_tools.write_to_txt_file(string_data, select_value, file_full_path)
#         return HttpResponse("success", content_type="application/json")
#     else:
#         return HttpResponse("false", content_type="application/json")


# @csrf_exempt
# def ajax_post_test(request):
#     if request.method == 'POST':
#         print("ajax test =============================")
#         return HttpResponse("哈哈哈哈哈哈哈哈哈", content_type="application/json")
#     else:
#         return HttpResponse("哈哈哈哈哈哈哈哈哈", content_type="application/json")


# def get_aaa(request, aaa):
#     # 使用aaa参数的值进行处理
#     print(aaa)
#     print(aaa)
#     return HttpResponse(aaa, content_type="application/json")
